import Dexie from "dexie";

export class Database extends Dexie{
  constructor() {
    super('database');

    // 注意：更改表字段，需要更新 version
    this.version(4).stores({
      operations: '++id, operationId, changes, timestamp',
    });

    this.operations = this.table('operations');
  }

  // 查询操作
  async getOperation(operationId = '') {
    if (!operationId) return '';
    return await this.operations.get({operationId});
  }

  // 新增操作
  async addOperation(params = {}) {
    const {operationId = '', changes = [], timestamp} = params;
    await this.operations.put({
      operationId: operationId,
      changes: changes,
      timestamp: timestamp,
    });
  }

  // 删除指定操作
  async deleteOperation(operationId = '') {
    if (!operationId) return '';
    await this.operations.where('operationId').equals(operationId).delete();
  }

  // 删除所有操作
  async deleteAllOperation() {
    await this.operations.clear();
  }
}
